package com.lcn.comm.framework.task;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @create: 下午5:18
 */
public class ConditionUtils {

    private static ConditionUtils instance = null;

    private static Map<String, Task> taskMap = new ConcurrentHashMap<String, Task>();

    private ConditionUtils() {
    }

    public static ConditionUtils getInstance() {
        if (instance == null) {
            synchronized (ConditionUtils.class) {
                if (instance == null) {
                    instance = new ConditionUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 创建task
     *
     * @param requestId
     * @return
     */
    public Task createTask(String requestId) {
        Task task = new Task();
        task.setRequestId(requestId);
        taskMap.put(requestId, task);
        return task;
    }

    /**
     * 获取task
     *
     * @param requestId
     * @return
     */
    public Task getTask(String requestId) {
        return taskMap.get(requestId);
    }

    /**
     * 删除task
     *
     * @param key
     */
    public void removeTask(String key) {
        if (StringUtils.isNotEmpty(key)) {
            taskMap.remove(key);
        }
    }

    /**
     * 根据key判断是否存在task
     *
     * @param key
     * @return
     */
    public boolean hasTask(String key) {
        return taskMap.containsKey(key);
    }

}
